﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using System.Threading.Tasks;

namespace 反射.Controllers
{
    public class RefectHelper
    {
        #region  反射属性
        //反射属性
        public static string ShowAttr(Type t)
        {
            PropertyInfo[] fs = t.GetProperties();
            string str = "";
            foreach (PropertyInfo item in fs)
            {
                str += "-" + item.Name;
            }
            return "这是tb_article返回的属性:" + str;
        }
        #endregion

        #region 反射属性值
        //反射属性值
        public static object ShowAttrValue(string attr, object obj)
        {
            object returnObj = new object();
            Type t = obj.GetType();
            PropertyInfo[] pros = t.GetProperties();
            foreach (PropertyInfo item in pros)
            {
                if (item.Name.ToLower() == attr.ToLower())
                {
                    returnObj = item.GetValue(obj);

                }
            }
            return "这是tb_article返回的属性值title:" + returnObj;
        }
        #endregion

        #region 反射字段
        //反射字段
        public static string ShowField(Type t)
        {
            FieldInfo[] fs = t.GetFields();
            string str = "";
            foreach (FieldInfo item in fs)
            {
                str += "-" + item.Name;
            }
            return "这是tb_article返回的字段:" + str;
        }
        #endregion

        #region 反射方法
        //反射方法
        public static string ShowMethod(Type t)
        {

            MethodInfo[] methInfo = t.GetMethods();
            string str = "";
            foreach (MethodInfo item in methInfo)
            {
                str += "-" + item.Name;
            }
            return "这是tb_article返回的方法:" + str;
        }
        #endregion

        #region 方法返回值

        //方法返回值
        public static object ReturnValue(Type t)
        {
            MethodInfo[] methods = t.GetMethods();
            object str = "";
            foreach (MethodInfo item in methods)
            {
                str += "|" + item.ReturnType.FullName;
            }
            return "这是tb_article方法的返回值类型:" + str;
        }
        #endregion

        #region 根据程序集(命名空间)返回类,接口等
        public static List<string> GetNameSpaceAll(string nameSpace, bool isClass = false, bool isIsInterface = false)
        {
            string str = string.Empty;

            Assembly assembly = Assembly.Load(nameSpace);

            // Type t = ass.GetType("Entity.tb_article"); List<Type>
            //tb_article news1 = (tb_article)ass.CreateInstance("Entity.tb_article");

            IEnumerable<Type> list = assembly.GetTypes().Where(c => 1 == 1);
            if (isClass)
                list = list.Where(c => c.IsClass);
            if (isIsInterface)
                list = list.Where(c => c.IsInterface);

            return list.Select(c => c.Name).ToList();
        }
        #endregion

        #region 调用方法
        public static void InvokeMenthod(string FullName, int[] arr)
        {
            Type type = Type.GetType(FullName);
            object obj = Activator.CreateInstance(type, arr);
            MethodInfo sortMethod = type.GetMethod("Sort");
            PropertyInfo array = type.GetProperty("arr");
            array.SetValue(obj, arr);
            sortMethod.Invoke(obj, null);
        }
        #endregion

    }
}
